package org.zuel.lkq.service.impl;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.zuel.lkq.domain.User;
import org.zuel.lkq.domain.UserExample;
import org.zuel.lkq.domain.UserRole;
import org.zuel.lkq.domain.UserRoleExample;
import org.zuel.lkq.enums.RoleEnum;
import org.zuel.lkq.mapper.UserMapper;
import org.zuel.lkq.mapper.UserRoleMapper;
import org.zuel.lkq.param.user.manage.QueryUserParam;
import org.zuel.lkq.param.user.manage.UserCreateParam;
import org.zuel.lkq.param.user.manage.UserDetailParam;
import org.zuel.lkq.param.user.manage.UserStatusParam;
import org.zuel.lkq.service.UserManageService;
import org.zuel.lkq.util.ResponseUtil;

import java.util.List;

@Service
public class UserManageServiceImpl implements UserManageService {

    public final UserMapper userMapper;

    public final UserRoleMapper userRoleMapper;

    @Autowired
    public UserManageServiceImpl(UserMapper userMapper, UserRoleMapper userRoleMapper) {

        this.userMapper = userMapper;
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public Object queryExistUser(QueryUserParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        UserExample example = new UserExample();
        example.setOrderByClause("add_time desc");
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (param.getUsername() != null) {
            criteria.andUsernameLike("%" + param.getUsername() + "%");
        }
        if (param.getStatus() != null) {
            criteria.andStatusEqualTo(param.getStatus());
        }
        List<User> users = userMapper.selectByExample(example);
        for (int i = 0; i < users.size(); ++i) {
            User user = users.get(i);
            UserRoleExample urExample = new UserRoleExample();
            UserRoleExample.Criteria urCriteria = urExample.createCriteria();
            urCriteria.andUserIdEqualTo(user.getId());
            UserRole ur = userRoleMapper.selectOneByExample(urExample);
            if (ur.getRoleId().equals(RoleEnum.ADMIN.getType())) {
                users.remove(i);
            }
        }
        return ResponseUtil.ok(users);
    }

    @Override
    public Object updateUserStatus(UserStatusParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        User user = new User();
        BeanUtils.copyProperties(param, user);
        userMapper.updateByPrimaryKeySelective(user);
        return ResponseUtil.ok("成功！");
    }

    @Override
    public Object getUserDetail(UserDetailParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        User user = userMapper.selectByPrimaryKey(param.getId());
        return ResponseUtil.ok(user);
    }

    @Override
    public Object editExistUser(UserCreateParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        User editUser = new User();
        BeanUtils.copyProperties(param, editUser);
        userMapper.updateByPrimaryKeySelective(editUser);
        return ResponseUtil.ok("更新成功！");
    }
}
